package com.xzmzhp.dao.maintain.sql;

import com.xzmzhp.utils.DateUtil;
import com.xzmzhp.utils.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Date;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 17:19
 * @program: ams
 * @description: 借出记录相关sql
 */
public class LendSql {

    /**
     * 根据参数查询数据sql
     * @param param 参数
     *        itemId 物品id
     *        itemName 物品名称
     *        stdmodeId  物品类型id
     *        personnelName 借出人名称
     *        borrowerName 借出人名称
     *        lendStatus 借用状态
     *        lendType 借用状态
     *        startTime 开始时间
     *        endTime  结束时间
     * @return 查询数据sql
     */
    public String findLendByParam(Map<String,Object> param){
        //物品id
        Integer itemId = Integer.parseInt(param.get("itemId").toString());
        //物品名称
        String itemName = param.get("itemName").toString();
        //物品类型id
        Integer stdmodeId = Integer.parseInt(param.get("stdmodeId").toString());
        //借出人名称
        String personnelName = param.get("personnelName").toString();
        //借出人名称
        String borrowerName = param.get("borrowerName").toString();
        //借用类型
        Integer borrowType = Integer.parseInt(param.get("borrowType").toString());
        //借用状态
        Integer lendStatus = Integer.parseInt(param.get("lendStatus").toString());
        //借出时间
        Date startTime = DateUtil.getDateTime(DateUtil.FORMAT_SHORT,param.get("startTime").toString());
        Date endTime = DateUtil.getDateTime(DateUtil.FORMAT_SHORT,param.get("endTime").toString());
        return new SQL(){
            {
                SELECT("lend_id,item_id,item_name,stdmode_id,stdmode_name,personnel_id,personnel.personnel_name,lend_type" +
                        ",borrower_name,borrow_type,lend_date,lend_status");
                FROM("lend,personnel");
                WHERE("lend.personnel_id = personnel.personnel_id");
                if (StringUtils.isNotEmpty(itemName)) {
                    WHERE("item_name = '%" + itemName + "%'");
                }
                if (StringUtils.isNotNull(itemId)) {
                    WHERE("item_id = " + itemId);
                }
                if (StringUtils.isNotNull(stdmodeId)) {
                    WHERE("stdmode_id = " + stdmodeId);
                }
                if (StringUtils.isNotEmpty(personnelName)) {
                    WHERE("personnel.personnel_name = '%" + personnelName + "%'");
                }
                if (StringUtils.isNotEmpty(borrowerName)) {
                    WHERE("borrower_name = '%" + borrowerName + "%'");
                }
                if (StringUtils.isNotNull(borrowType)) {
                    WHERE("borrow_type = " + borrowType);
                }
                if (StringUtils.isNotNull(lendStatus)) {
                    WHERE("lend_status = " + lendStatus);
                }
                if (StringUtils.isNotNull(startTime)) {
                    WHERE("lend_date >= " + startTime);
                }
                if (StringUtils.isNotNull(endTime)) {
                    WHERE("lend_date <= " + endTime);
                }
                ORDER_BY("lend_date desc");
            }
        }.toString();
    }
}
